﻿using AntdUI;
using DataTurnClient.Model;
using DataTurnClient.ViewModel;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataTurnClient.Controls
{
    public partial class RouteView : UserControl, IViewFor<RouteViewModel>
    {
        object IViewFor.ViewModel
        {
            get => ViewModel;
            set => ViewModel = (RouteViewModel)value;
        }

        public RouteViewModel ViewModel { get; set; }
        public RouteView()
        {
            InitializeComponent();
            InitBindings();
            InitColumns();
        }

        private void InitBindings()
        {
            ViewModel = new RouteViewModel();
            this.WhenActivated(d =>
            {
                this.badge1.Events().Click.Do(x =>
                {
                    this.Dispose();
                }).Subscribe().DisposeWith(d);

                this.Events().Load.Do(x =>
                {
                    ViewModel.SelectLineCommand.Execute().Subscribe();
                }).Subscribe();

                this.button_add.Events().Click.Do(x =>
                {
                    var line = new RouteLine();
                    line.CellLinks = new AntdUI.CellLink[]
{
                            new CellButton(Guid.NewGuid().ToString(),"编辑",TTypeMini.Success),
                            new CellButton(Guid.NewGuid().ToString(),"删除",TTypeMini.Error),
};
                    ViewModel.RouteList.Add(line);
                    table_line.EditMode = TEditMode.Click;
                }).Subscribe().DisposeWith(d);

                this.table_line.CellButtonClick += (s, e) =>
                {
                    var buttontext = e.Btn.Text;
                    if (e.Record is RouteLine line)
                    {
                        switch (buttontext)
                        {
                            case "编辑":
                                table_line.EditMode = TEditMode.Click;
                                table_line.EnterEditMode(e.RowIndex,1);
                                break;
                            case "删除":
                                var config = new AntdUI.Modal.Config(Globals.MainForm,"提醒",$"是否删除该线路,{line.Ip},{line.Port},{line.Path}");
                                config.Padding = new Size(18,18);
                                var dialog = AntdUI.Modal.open(config);
                                if (dialog == DialogResult.OK)
                                {
                                    ViewModel.DeleteLineCommand.Execute(line).Subscribe(result =>
                                    {
                                        if (result.Success)
                                        {
                                            ViewModel.SelectLineCommand.Execute().Subscribe();
                                        }
                                    });
                                }
                                break;
                        }
                    }
                };

                table_line.CellEditComplete += (s, e) =>
                {
                    var route = e.Record as RouteLine;
                    ViewModel.UpdateLineCommand.Execute(route).Subscribe(result =>
                    {
                        if (!result.Success)
                        {
                            var configDelete = new AntdUI.Notification.Config(Globals.MainForm, "提醒", "未更改", TType.Error, TAlignFrom.Top);
                            AntdUI.Notification.open(configDelete);
                        }
                    });
                };

            });
        }
        private void InitColumns()
        {
            table_line.Columns = new AntdUI.ColumnCollection()
            {
                new AntdUI.Column("Ip","地址"),
                new AntdUI.Column("Port","端口"),
                new AntdUI.Column("Path","路径"),
                new Column("CellLinks", "操作",ColumnAlign.Center){ Fixed = true },
            };
            table_line.Binding(ViewModel.RouteList);
        }
    }
}
